//
//  TabBarController.m
//  MyFrameWork
//
//  Created by eason on 16/4/21.
//  Copyright © 2016年 普惠. All rights reserved.
//

#import "TabBarController.h"
#import "TabBar.h"
#import "TabBarItem.h"

@interface TabBarController ()<TabBarDelegate>

@property (nonatomic, strong) TabBar * tabbar;
@end

@implementation TabBarController

- (UIColor *)itemTitleColor {
    
    if (!_itemTitleColor) {
        
        _itemTitleColor = CREATECOLOR(177, 177, 177, 1);
    }
    return _itemTitleColor;
}

- (UIColor *)selectedItemTitleColor {
    
    if (!_selectedItemTitleColor) {
        
        _selectedItemTitleColor = CREATECOLOR(234, 103, 7, 1);
    }
    return _selectedItemTitleColor;
}

- (UIFont *)itemTitleFont {
    
    if (!_itemTitleFont) {
        
        _itemTitleFont = [UIFont systemFontOfSize:10.0f];
    }
    return _itemTitleFont;
}

- (UIFont *)badgeTitleFont {
    
    if (!_badgeTitleFont) {
        
        _badgeTitleFont = [UIFont systemFontOfSize:11.0f];
    }
    return _badgeTitleFont;
}

#pragma mark -

- (void)loadView {
    
    [super loadView];
    
    self.itemImageRatio = 0.70f;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self.tabBar addSubview:({
        
        TabBar *tabBar = [[TabBar alloc] init];
        tabBar.frame     = self.tabBar.bounds;
        tabBar.delegate  = self;
        
        self.tabbar = tabBar;
    })];
    //    UIKeyboardWillShowNotification
    //    [[NSNotificationCenter defaultCenter] addobserver];
}

//- (void)viewWillLayoutSubviews {
//
//    [super viewWillLayoutSubviews];
//
//
//}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self removeOriginControls];
}

- (void)removeOriginControls {
    
    [self.tabBar.subviews enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
        if ([obj isKindOfClass:[UIControl class]]) {
            
            [obj removeFromSuperview];
        }
    }];
}

- (void)setViewControllers:(NSArray *)viewControllers {
    
    self.tabbar.badgeTitleFont         = self.badgeTitleFont;
    self.tabbar.itemTitleFont          = self.itemTitleFont;
    self.tabbar.itemImageRatio         = self.itemImageRatio;
    self.tabbar.itemTitleColor         = self.itemTitleColor;
    self.tabbar.selectedItemTitleColor = self.selectedItemTitleColor;
    
    self.tabbar.tabBarItemCount = viewControllers.count;
    
    [viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
        UIViewController *VC = (UIViewController *)obj;
        
        UIImage *selectedImage = VC.tabBarItem.selectedImage;
        VC.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        [self addChildViewController:VC];
        
        [self.tabbar addTabBarItem:VC.tabBarItem];
    }];
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    
    [super setSelectedIndex:selectedIndex];
    
    self.tabbar.selectedItem.selected = NO;
    self.tabbar.selectedItem = self.tabbar.tabBarItems[selectedIndex];
    self.tabbar.selectedItem.selected = YES;
}

#pragma mark - TabBarDelegate Method

- (void)tabBar:(TabBar *)tabBarView didSelectedItemFrom:(NSInteger)from to:(NSInteger)to {
    
    self.selectedIndex = to;
}

@end
